<?PHP
if(!defined('URLFZ'))
	die('Hacking Attempt');

//takes in a url and determins if it has already ben shortened.
function alreadyShort($url, $con = SQL_CON){
$query = mysql_query('SELECT id FROM ' . DB_TABLE. ' WHERE long_url="' . mysql_real_escape_string($url) . '"');
	if(mysql_num_rows($query) == 1){
		$already_shortened = mysql_result($query, 0);
	}
	return $already_shortened;
}

function shortenURL($url, $con = SQL_CON){
	mysql_query('LOCK TABLES ' . DB_TABLE . ' WRITE;');
	mysql_query('INSERT INTO ' . DB_TABLE . ' (long_url, created, creator) VALUES ("' . mysql_real_escape_string($url) . '", "' . time() . '", "' . mysql_real_escape_string($_SERVER['REMOTE_ADDR']) . '")');
	$shortened_url = getShortenedURLFromID(mysql_insert_id());
	mysql_query('UNLOCK TABLES');
	return $shortened_url;
}

function track($id){
mysql_query('UPDATE ' . DB_TABLE . ' SET referrals=referrals+1 WHERE id="' . mysql_real_escape_string($id) . '"');
}

function getLong($id){
	$eid = mysql_real_escape_string($id);
	$query = mysql_query('SELECT long_url FROM ' . DB_TABLE . ' WHERE id="' . $eid . '"');
	if(mysql_num_rows($query) == 1){
		$result = mysql_result($query, 0);
	}
	return $result;
}
?>
